<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">

    <div class="article-intro" id="content">

        <h1>Python3  <span class="color_h1">日期和时间</span></h1>

        <p>Python 程序能用很多方式处理日期和时间，转换日期格式是一个常见的功能。</p>
        <p>Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间。</p>


        <p>时间间隔是以秒为单位的浮点小数。</p>
        <p>每个时间戳都以自从1970年1月1日午夜（历元）经过了多长时间来表示。</p>
        <p>Python 的 time 模块下有很多函数可以转换常见日期格式。如函数time.time()用于获取当前时间戳, 如下实例:</p><p>
    </p><pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> time</span><span class="pun">;</span><span class="pln">  </span><span class="com"># 引入time模块</span><span class="pln">

ticks </span><span class="pun">=</span><span class="pln"> time</span><span class="pun">.</span><span class="pln">time</span><span class="pun">()</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"当前时间戳为:"</span><span class="pun">,</span><span class="pln"> ticks</span><span class="pun">)</span></pre>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">当前时间戳为:</span><span class="pln"> </span><span class="lit">1459996086.7115328</span></pre>

        <p>
            时间戳单位最适于做日期运算。但是1970年之前的日期就无法以此表示了。太遥远的日期也不行，UNIX和Windows只支持到2038年。
        </p>
        <br>
        <h2>什么是时间元组？</h2>
        <p>很多Python函数用一个元组装起来的9组数字处理时间:</p>
        <table class="reference">
            <tbody><tr>
                <th style="width:10%">序号
                </th><th style="width:40%">字段</th><th>值</th>
            </tr>
            <tr><td>0</td><td>4位数年</td><td>2008</td></tr>
            <tr><td>1</td><td>月</td><td>1 到 12</td></tr>
            <tr><td>2</td><td>日</td><td>1到31</td></tr>
            <tr><td>3</td><td>小时</td><td>0到23</td></tr>
            <tr><td>4</td><td>分钟</td><td>0到59</td></tr>
            <tr><td>5</td><td>秒</td><td>0到61 (60或61 是闰秒)</td></tr>
            <tr><td>6</td><td>一周的第几日</td><td>0到6 (0是周一)</td></tr>
            <tr><td>7</td><td>一年的第几日</td><td>1到366 (儒略历)</td></tr>
            <tr><td>8</td><td>夏令时</td><td>-1, 0, 1, -1是决定是否为夏令时的旗帜</td></tr>
            </tbody></table>
        <p>上述也就是struct_time元组。这种结构具有如下属性：</p>
        <table class="reference">
            <tbody><tr>
                <th style="width:10%">序号</th><th style="width:40%">属性</th><th>值</th>
            </tr>
            <tr><td>0</td><td>tm_year</td><td>2008</td></tr>
            <tr><td>1</td><td>tm_mon</td><td>1 到 12</td></tr>
            <tr><td>2</td><td>tm_mday</td><td>1 到 31</td></tr>
            <tr><td>3</td><td>tm_hour</td><td>0 到 23</td></tr>
            <tr><td>4</td><td>tm_min</td><td>0 到 59</td></tr>
            <tr><td>5</td><td>tm_sec</td><td>0 到 61 (60或61 是闰秒)</td></tr>
            <tr><td>6</td><td>tm_wday</td><td>0到6 (0是周一)</td></tr>
            <tr><td>7</td><td>tm_yday</td><td>一年中的第几天，1 到 366</td></tr>
            <tr><td>8</td><td>tm_isdst</td><td>是否为夏令时，值有：1(夏令时)、0(不是夏令时)、-1(未知)，默认 -1 </td></tr>
            </tbody></table>
        <br>
        <h2>获取当前时间</h2>
        <p>从返回浮点数的时间辍方式向时间元组转换，只要将浮点数传递给如localtime之类的函数。</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> time

localtime </span><span class="pun">=</span><span class="pln"> time</span><span class="pun">.</span><span class="pln">localtime</span><span class="pun">(</span><span class="pln">time</span><span class="pun">.</span><span class="pln">time</span><span class="pun">())</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"本地时间为 :"</span><span class="pun">,</span><span class="pln"> localtime</span><span class="pun">)</span></pre>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">本地时间为</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> time</span><span class="pun">.</span><span class="pln">struct_time</span><span class="pun">(</span><span class="pln">tm_year</span><span class="pun">=</span><span class="lit">2016</span><span class="pun">,</span><span class="pln"> tm_mon</span><span class="pun">=</span><span class="lit">4</span><span class="pun">,</span><span class="pln"> tm_mday</span><span class="pun">=</span><span class="lit">7</span><span class="pun">,</span><span class="pln"> tm_hour</span><span class="pun">=</span><span class="lit">10</span><span class="pun">,</span><span class="pln"> tm_min</span><span class="pun">=</span><span class="lit">28</span><span class="pun">,</span><span class="pln"> tm_sec</span><span class="pun">=</span><span class="lit">49</span><span class="pun">,</span><span class="pln"> tm_wday</span><span class="pun">=</span><span class="lit">3</span><span class="pun">,</span><span class="pln"> tm_yday</span><span class="pun">=</span><span class="lit">98</span><span class="pun">,</span><span class="pln"> tm_isdst</span><span class="pun">=</span><span class="lit">0</span><span class="pun">)</span></pre>
        <br>
        <h2>获取格式化的时间</h2>
        <p>你可以根据需求选取各种格式，但是最简单的获取可读的时间模式的函数是asctime():</p>

        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> time

localtime </span><span class="pun">=</span><span class="pln"> time</span><span class="pun">.</span><span class="pln">asctime</span><span class="pun">(</span><span class="pln"> time</span><span class="pun">.</span><span class="pln">localtime</span><span class="pun">(</span><span class="pln">time</span><span class="pun">.</span><span class="pln">time</span><span class="pun">())</span><span class="pln"> </span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"本地时间为 :"</span><span class="pun">,</span><span class="pln"> localtime</span><span class="pun">)</span></pre>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">本地时间为</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Apr</span><span class="pln">  </span><span class="lit">7</span><span class="pln"> </span><span class="lit">10</span><span class="pun">:</span><span class="lit">29</span><span class="pun">:</span><span class="lit">13</span><span class="pln"> </span><span class="lit">2016</span></pre>

        <h2>格式化日期</h2>
        <p>我们可以使用 time 模块的 strftime 方法来格式化日期，：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">time</span><span class="pun">.</span><span class="pln">strftime</span><span class="pun">(</span><span class="pln">format</span><span class="pun">[,</span><span class="pln"> t</span><span class="pun">])</span></pre>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> time

</span><span class="com"># 格式化成2016-03-20 11:45:39形式</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="pln">time</span><span class="pun">.</span><span class="pln">strftime</span><span class="pun">(</span><span class="str">"%Y-%m-%d %H:%M:%S"</span><span class="pun">,</span><span class="pln"> time</span><span class="pun">.</span><span class="pln">localtime</span><span class="pun">()))</span><span class="pln">

</span><span class="com"># 格式化成Sat Mar 28 22:24:24 2016形式</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="pln">time</span><span class="pun">.</span><span class="pln">strftime</span><span class="pun">(</span><span class="str">"%a %b %d %H:%M:%S %Y"</span><span class="pun">,</span><span class="pln"> time</span><span class="pun">.</span><span class="pln">localtime</span><span class="pun">()))</span><span class="pln">

</span><span class="com"># 将格式字符串转换为时间戳</span><span class="pln">
a </span><span class="pun">=</span><span class="pln"> </span><span class="str">"Sat Mar 28 22:24:24 2016"</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="pln">time</span><span class="pun">.</span><span class="pln">mktime</span><span class="pun">(</span><span class="pln">time</span><span class="pun">.</span><span class="pln">strptime</span><span class="pun">(</span><span class="pln">a</span><span class="pun">,</span><span class="str">"%a %b %d %H:%M:%S %Y"</span><span class="pun">)))</span></pre>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="lit">2016</span><span class="pun">-</span><span class="lit">04</span><span class="pun">-</span><span class="lit">07</span><span class="pln"> </span><span class="lit">10</span><span class="pun">:</span><span class="lit">29</span><span class="pun">:</span><span class="lit">46</span><span class="pln">
</span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Apr</span><span class="pln"> </span><span class="lit">07</span><span class="pln"> </span><span class="lit">10</span><span class="pun">:</span><span class="lit">29</span><span class="pun">:</span><span class="lit">46</span><span class="pln"> </span><span class="lit">2016</span><span class="pln">
</span><span class="lit">1459175064.0</span></pre>
        <p>python中时间日期格式化符号：</p>
        <ul> <li>%y 两位数的年份表示（00-99）</li> <li>%Y 四位数的年份表示（000-9999）</li> <li>%m 月份（01-12）</li> <li>%d 月内中的一天（0-31）</li> <li>%H 24小时制小时数（0-23）</li> <li>%I 12小时制小时数（01-12）</li> <li>%M 分钟数（00=59）</li> <li>%S 秒（00-59）</li> <li>%a 本地简化星期名称</li> <li>%A 本地完整星期名称</li> <li>%b 本地简化的月份名称</li> <li>%B 本地完整的月份名称</li> <li>%c 本地相应的日期表示和时间表示</li> <li>%j 年内的一天（001-366）</li> <li>%p 本地A.M.或P.M.的等价符</li> <li>%U 一年中的星期数（00-53）星期天为星期的开始</li> <li>%w 星期（0-6），星期天为星期的开始</li> <li>%W 一年中的星期数（00-53）星期一为星期的开始</li> <li>%x 本地相应的日期表示</li> <li>%X 本地相应的时间表示</li> <li>%Z 当前时区的名称</li> <li>%% %号本身</li> </ul>

        <h2>获取某月日历</h2>
        <p>Calendar模块有很广泛的方法用来处理年历和月历，例如打印某月的月历：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> calendar

cal </span><span class="pun">=</span><span class="pln"> calendar</span><span class="pun">.</span><span class="pln">month</span><span class="pun">(</span><span class="lit">2016</span><span class="pun">,</span><span class="pln"> </span><span class="lit">1</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"以下输出2016年1月份的日历:"</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="pln">cal</span><span class="pun">)</span></pre>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">以下输出</span><span class="lit">2016</span><span class="pun">年</span><span class="lit">1</span><span class="pun">月份的日历:</span><span class="pln">
    </span><span class="typ">January</span><span class="pln"> </span><span class="lit">2016</span><span class="pln">
</span><span class="typ">Mo</span><span class="pln"> </span><span class="typ">Tu</span><span class="pln"> </span><span class="typ">We</span><span class="pln"> </span><span class="typ">Th</span><span class="pln"> </span><span class="typ">Fr</span><span class="pln"> </span><span class="typ">Sa</span><span class="pln"> </span><span class="typ">Su</span><span class="pln">
             </span><span class="lit">1</span><span class="pln">  </span><span class="lit">2</span><span class="pln">  </span><span class="lit">3</span><span class="pln">
 </span><span class="lit">4</span><span class="pln">  </span><span class="lit">5</span><span class="pln">  </span><span class="lit">6</span><span class="pln">  </span><span class="lit">7</span><span class="pln">  </span><span class="lit">8</span><span class="pln">  </span><span class="lit">9</span><span class="pln"> </span><span class="lit">10</span><span class="pln">
</span><span class="lit">11</span><span class="pln"> </span><span class="lit">12</span><span class="pln"> </span><span class="lit">13</span><span class="pln"> </span><span class="lit">14</span><span class="pln"> </span><span class="lit">15</span><span class="pln"> </span><span class="lit">16</span><span class="pln"> </span><span class="lit">17</span><span class="pln">
</span><span class="lit">18</span><span class="pln"> </span><span class="lit">19</span><span class="pln"> </span><span class="lit">20</span><span class="pln"> </span><span class="lit">21</span><span class="pln"> </span><span class="lit">22</span><span class="pln"> </span><span class="lit">23</span><span class="pln"> </span><span class="lit">24</span><span class="pln">
</span><span class="lit">25</span><span class="pln"> </span><span class="lit">26</span><span class="pln"> </span><span class="lit">27</span><span class="pln"> </span><span class="lit">28</span><span class="pln"> </span><span class="lit">29</span><span class="pln"> </span><span class="lit">30</span><span class="pln"> </span><span class="lit">31</span></pre>
        <br>
        <h2>Time 模块</h2>
        <p>Time 模块包含了以下内置函数，既有时间处理相的，也有转换时间格式的：</p>

        <table class="reference">
            <tbody><tr>
                <th style="width:5%">序号</th><th style="width:35%">函数及描述</th><th>实例</th></tr>
            <tr><td>1</td><td>time.altzone<br>返回格林威治西部的夏令时地区的偏移秒数。如果该地区在格林威治东部会返回负值（如西欧，包括英国）。对夏令时启用地区才能使用。</td>
                <td>
                    <p>以下实例展示了 altzone()函数的使用方法：</p>
                    <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">import</span><span class="pln"> time
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"time.altzone %d "</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> time</span><span class="pun">.</span><span class="pln">altzone</span><span class="pun">)</span><span class="pln">
time</span><span class="pun">.</span><span class="pln">altzone </span><span class="pun">-</span><span class="lit">28800</span><span class="pln"> </span></pre>
                </td>
            </tr>
            <tr><td>2</td><td>time.asctime([tupletime])<br>接受时间元组并返回一个可读的形式为"Tue Dec 11 18:07:14 2008"（2008年12月11日&nbsp;周二18时07分14秒）的24个字符的字符串。</td>
                <td>
                    <p>以下实例展示了 asctime()函数的使用方法：</p>
                    <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">import</span><span class="pln"> time
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> t </span><span class="pun">=</span><span class="pln"> time</span><span class="pun">.</span><span class="pln">localtime</span><span class="pun">()</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"time.asctime(t): %s "</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> time</span><span class="pun">.</span><span class="pln">asctime</span><span class="pun">(</span><span class="pln">t</span><span class="pun">))</span><span class="pln">
time</span><span class="pun">.</span><span class="pln">asctime</span><span class="pun">(</span><span class="pln">t</span><span class="pun">):</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Apr</span><span class="pln">  </span><span class="lit">7</span><span class="pln"> </span><span class="lit">10</span><span class="pun">:</span><span class="lit">36</span><span class="pun">:</span><span class="lit">20</span><span class="pln"> </span><span class="lit">2016</span><span class="pln"> </span></pre>
                </td>

            </tr>
            <tr><td>3</td><td><a href="python3-att-time-clock.html" target="_blank">time.clock()</a><br>用以浮点数计算的秒数返回当前的CPU时间。用来衡量不同程序的耗时，比time.time()更有用。</td><td><a href="python3-att-time-clock.html" target="_blank">实例</a></td></tr>
            <tr><td>4</td><td>time.ctime([secs])<br>作用相当于asctime(localtime(secs))，未给参数相当于asctime()</td>
                <td>
                    <p>以下实例展示了 ctime()函数的使用方法：</p>
                    <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">import</span><span class="pln"> time
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"time.ctime() : %s"</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> time</span><span class="pun">.</span><span class="pln">ctime</span><span class="pun">())</span><span class="pln">
time</span><span class="pun">.</span><span class="pln">ctime</span><span class="pun">()</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="typ">Thu</span><span class="pln"> </span><span class="typ">Apr</span><span class="pln">  </span><span class="lit">7</span><span class="pln"> </span><span class="lit">10</span><span class="pun">:</span><span class="lit">51</span><span class="pun">:</span><span class="lit">58</span><span class="pln"> </span><span class="lit">2016</span></pre>
                </td>


            </tr>
            <tr><td>5</td><td>time.gmtime([secs])<br>接收时间辍（1970纪元后经过的浮点秒数）并返回格林威治天文时间下的时间元组t。注：t.tm_isdst始终为0</td>
                <td>
                    <p>以下实例展示了 gmtime()函数的使用方法：</p>
                    <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">import</span><span class="pln"> time
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"gmtime :"</span><span class="pun">,</span><span class="pln"> time</span><span class="pun">.</span><span class="pln">gmtime</span><span class="pun">(</span><span class="lit">1455508609.34375</span><span class="pun">))</span><span class="pln">
gmtime </span><span class="pun">:</span><span class="pln"> time</span><span class="pun">.</span><span class="pln">struct_time</span><span class="pun">(</span><span class="pln">tm_year</span><span class="pun">=</span><span class="lit">2016</span><span class="pun">,</span><span class="pln"> tm_mon</span><span class="pun">=</span><span class="lit">2</span><span class="pun">,</span><span class="pln"> tm_mday</span><span class="pun">=</span><span class="lit">15</span><span class="pun">,</span><span class="pln"> tm_hour</span><span class="pun">=</span><span class="lit">3</span><span class="pun">,</span><span class="pln"> tm_min</span><span class="pun">=</span><span class="lit">56</span><span class="pun">,</span><span class="pln"> tm_sec</span><span class="pun">=</span><span class="lit">49</span><span class="pun">,</span><span class="pln"> tm_wday</span><span class="pun">=</span><span class="lit">0</span><span class="pun">,</span><span class="pln"> tm_yday</span><span class="pun">=</span><span class="lit">46</span><span class="pun">,</span><span class="pln"> tm_isdst</span><span class="pun">=</span><span class="lit">0</span><span class="pun">)</span></pre>
                </td>
            </tr>
            <tr><td>6</td><td>time.localtime([secs]<br>接收时间辍（1970纪元后经过的浮点秒数）并返回当地时间下的时间元组t（t.tm_isdst可取0或1，取决于当地当时是不是夏令时）。</td>
                <td>
                    <p>以下实例展示了 localtime()函数的使用方法：</p>
                    <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">import</span><span class="pln"> time
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"localtime(): "</span><span class="pun">,</span><span class="pln"> time</span><span class="pun">.</span><span class="pln">localtime</span><span class="pun">(</span><span class="lit">1455508609.34375</span><span class="pun">))</span><span class="pln">
localtime</span><span class="pun">():</span><span class="pln">  time</span><span class="pun">.</span><span class="pln">struct_time</span><span class="pun">(</span><span class="pln">tm_year</span><span class="pun">=</span><span class="lit">2016</span><span class="pun">,</span><span class="pln"> tm_mon</span><span class="pun">=</span><span class="lit">2</span><span class="pun">,</span><span class="pln"> tm_mday</span><span class="pun">=</span><span class="lit">15</span><span class="pun">,</span><span class="pln"> tm_hour</span><span class="pun">=</span><span class="lit">11</span><span class="pun">,</span><span class="pln"> tm_min</span><span class="pun">=</span><span class="lit">56</span><span class="pun">,</span><span class="pln"> tm_sec</span><span class="pun">=</span><span class="lit">49</span><span class="pun">,</span><span class="pln"> tm_wday</span><span class="pun">=</span><span class="lit">0</span><span class="pun">,</span><span class="pln"> tm_yday</span><span class="pun">=</span><span class="lit">46</span><span class="pun">,</span><span class="pln"> tm_isdst</span><span class="pun">=</span><span class="lit">0</span><span class="pun">)</span></pre>
                </td>
            </tr>
            <tr><td>7</td><td><a href="python3-att-time-mktime.html" target="_blank">time.mktime(tupletime)</a><br>接受时间元组并返回时间辍（1970纪元后经过的浮点秒数）。</td><td><a href="python3-att-time-mktime.html" target="_blank">实例</a></td></tr>
            <tr><td>8</td><td>time.sleep(secs)<br>推迟调用线程的运行，secs指秒数。</td>
                <td>
                    <p>以下实例展示了 sleep()函数的使用方法：</p>
                    <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> time

</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Start : %s"</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> time</span><span class="pun">.</span><span class="pln">ctime</span><span class="pun">())</span><span class="pln">
time</span><span class="pun">.</span><span class="pln">sleep</span><span class="pun">(</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"End : %s"</span><span class="pln"> </span><span class="pun">%</span><span class="pln"> time</span><span class="pun">.</span><span class="pln">ctime</span><span class="pun">())</span></pre>
                </td>

            </tr>
            <tr><td>9</td><td>time.strftime(fmt[,tupletime])<br>接收以时间元组，并返回以可读字符串表示的当地时间，格式由fmt决定。</td>
                <td>
                    <p>以下实例展示了 strftime()函数的使用方法：</p>
                    <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">import</span><span class="pln"> time
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="pln">time</span><span class="pun">.</span><span class="pln">strftime</span><span class="pun">(</span><span class="str">"%Y-%m-%d %H:%M:%S"</span><span class="pun">,</span><span class="pln"> time</span><span class="pun">.</span><span class="pln">localtime</span><span class="pun">()))</span><span class="pln">
</span><span class="lit">2016</span><span class="pun">-</span><span class="lit">04</span><span class="pun">-</span><span class="lit">07</span><span class="pln"> </span><span class="lit">11</span><span class="pun">:</span><span class="lit">18</span><span class="pun">:</span><span class="lit">05</span></pre>
                </td>
            </tr>
            <tr><td>10</td><td>time.strptime(str,fmt='%a %b %d %H:%M:%S %Y')<br>根据fmt的格式把一个时间字符串解析为时间元组。</td>
                <td>
                    <p>以下实例展示了 strftime()函数的使用方法：</p>
                    <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">import</span><span class="pln"> time
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> struct_time </span><span class="pun">=</span><span class="pln"> time</span><span class="pun">.</span><span class="pln">strptime</span><span class="pun">(</span><span class="str">"30 Nov 00"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"%d %b %y"</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"返回元组: "</span><span class="pun">,</span><span class="pln"> struct_time</span><span class="pun">)</span><span class="pln">
</span><span class="pun">返回元组:</span><span class="pln">  time</span><span class="pun">.</span><span class="pln">struct_time</span><span class="pun">(</span><span class="pln">tm_year</span><span class="pun">=</span><span class="lit">2000</span><span class="pun">,</span><span class="pln"> tm_mon</span><span class="pun">=</span><span class="lit">11</span><span class="pun">,</span><span class="pln"> tm_mday</span><span class="pun">=</span><span class="lit">30</span><span class="pun">,</span><span class="pln"> tm_hour</span><span class="pun">=</span><span class="lit">0</span><span class="pun">,</span><span class="pln"> tm_min</span><span class="pun">=</span><span class="lit">0</span><span class="pun">,</span><span class="pln"> tm_sec</span><span class="pun">=</span><span class="lit">0</span><span class="pun">,</span><span class="pln"> tm_wday</span><span class="pun">=</span><span class="lit">3</span><span class="pun">,</span><span class="pln"> tm_yday</span><span class="pun">=</span><span class="lit">335</span><span class="pun">,</span><span class="pln"> tm_isdst</span><span class="pun">=-</span><span class="lit">1</span><span class="pun">)</span></pre>
                </td>
            </tr>
            <tr><td>11</td><td>time.time( )<br>返回当前时间的时间戳（1970纪元后经过的浮点秒数）。</td>

                <td>
                    <p>以下实例展示了 time()函数的使用方法：</p>
                    <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">import</span><span class="pln"> time
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span class="pun">(</span><span class="pln">time</span><span class="pun">.</span><span class="pln">time</span><span class="pun">())</span><span class="pln">
</span><span class="lit">1459999336.1963577</span></pre>
                </td>
            </tr>
            <tr><td>12</td><td><a href="python3-att-time-tzset.html" target="_blank">time.tzset()</a><br>根据环境变量TZ重新初始化时间相关设置。 </td><td><a href="python3-att-time-tzset.html" target="_blank">实例</a></td></tr>
            </tbody></table>
        <p>Time模块包含了以下2个非常重要的属性：</p>

        <table class="reference">
            <tbody><tr>
                <th style="width:5%">序号</th><th style="width:95%">属性及描述</th></tr>
            <tr><td>1</td><td><b>time.timezone</b><br>属性time.timezone是当地时区（未启动夏令时）距离格林威治的偏移秒数（&gt;0，美洲;&lt;=0大部分欧洲，亚洲，非洲）。</td></tr>
            <tr><td>2</td><td><b>time.tzname</b><br>属性time.tzname包含一对根据情况的不同而不同的字符串，分别是带夏令时的本地时区名称，和不带的。</td></tr>
            </tbody></table>
        <br>
        <h2>日历（Calendar）模块</h2>
        <p>此模块的函数都是日历相关的，例如打印某月的字符月历。</p>
        <p>星期一是默认的每周第一天，星期天是默认的最后一天。更改设置需调用calendar.setfirstweekday()函数。模块包含了以下内置函数：</p>
        <table class="reference">
            <tbody><tr>
                <th style="width:5%">序号</th><th style="width:95%">函数及描述</th></tr>
            <tr><td>1</td><td><b>calendar.calendar(year,w=2,l=1,c=6)</b><br>返回一个多行字符串格式的year年年历，3个月一行，间隔距离为c。 每日宽度间隔为w字符。每行长度为21* W+18+2* C。l是每星期行数。</td></tr>
            <tr><td>2</td><td><b>calendar.firstweekday( )</b><br>返回当前每周起始日期的设置。默认情况下，首次载入caendar模块时返回0，即星期一。</td></tr>
            <tr><td>3</td><td><b>calendar.isleap(year)</b><br>是闰年返回True，否则为false。</td></tr>
            <tr><td>4</td><td><b>calendar.leapdays(y1,y2)</b><br>返回在Y1，Y2两年之间的闰年总数。</td></tr>
            <tr><td>5</td><td><b>calendar.month(year,month,w=2,l=1)</b><br>返回一个多行字符串格式的year年month月日历，两行标题，一周一行。每日宽度间隔为w字符。每行的长度为7* w+6。l是每星期的行数。</td></tr>
            <tr><td>6</td><td><b>calendar.monthcalendar(year,month)</b><br>返回一个整数的单层嵌套列表。每个子列表装载代表一个星期的整数。Year年month月外的日期都设为0;范围内的日子都由该月第几日表示，从1开始。</td></tr>
            <tr><td>7</td><td><b>calendar.monthrange(year,month)</b><br>返回两个整数。第一个是该月的星期几的日期码，第二个是该月的日期码。日从0（星期一）到6（星期日）;月从1到12。</td></tr>
            <tr><td>8</td><td><b>calendar.prcal(year,w=2,l=1,c=6)</b><br>相当于 print calendar.calendar(year,w,l,c).</td></tr>
            <tr><td>9</td><td><b>calendar.prmonth(year,month,w=2,l=1)</b><br>相当于 print calendar.calendar（year，w，l，c）。</td></tr>
            <tr><td>10</td><td><b>calendar.setfirstweekday(weekday)</b><br>设置每周的起始日期码。0（星期一）到6（星期日）。</td></tr>
            <tr><td>11</td><td><b>calendar.timegm(tupletime)</b><br>和time.gmtime相反：接受一个时间元组形式，返回该时刻的时间辍（1970纪元后经过的浮点秒数）。</td></tr>
            <tr><td>12</td><td><b>calendar.weekday(year,month,day)</b><br>返回给定日期的日期码。0（星期一）到6（星期日）。月份为 1（一月） 到 12（12月）。</td></tr>
            </tbody></table>
        <br>
        <h2>其他相关模块和函数</h2>
        <p>在Python中，其他处理日期和时间的模块还有：</p>

        <ul>
            <li><a href="https://docs.python.org/3/library/time.html" target="_blank" rel="nofollow">time 模块</a></li>
            <li><a href="https://docs.python.org/3/library/datetime.html" target="_blank" rel="nofollow">datetime模块</a></li>
        </ul>

    </div>
</div>

</body>
</html>
